﻿using System;
using System.Threading.Tasks;
using ZeroBugBounce.Missive.Locks;
using System.Threading;

namespace ZeroBugBounce.Missive
{
	public abstract class Handler
	{
		public Topic Topic { get; set; }
		public DispatchMode DispatchMode { get; set; }
		public abstract Type HandledType { get; }
		public Boolean Cancelable { get; protected set; }
	}

	public abstract class Handler<T> : Handler
	{
		public Handler()
		{
			Topic = Topic.Default;
		}

		public Handler(Topic topic, DispatchMode dispatchMode)
		{
			Topic = topic;
			DispatchMode = dispatchMode;
		}

		public abstract void Handle(HandlerContext<T> context);

		public override Type HandledType { get { return typeof(T); } }
	}

	public class LambdaHandler<T> : Handler<T>
	{
		Action<HandlerContext<T>> handlerContext;

		public LambdaHandler(Topic topic, DispatchMode dispatchMode, Boolean isCancelable, Action<HandlerContext<T>> action)
		{
			handlerContext = action;
			Topic = topic;
			DispatchMode = dispatchMode;
			Cancelable = isCancelable;
		}

		public override void Handle(HandlerContext<T> context)
		{
			handlerContext(context);
		}
	}
}
